package com.example.demo.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.service.MyAccessTokenService;
import com.example.demo.service.UserService;
import com.example.demo.model.MyAccessToken;
import com.example.demo.model.User;

import springfox.documentation.annotations.ApiIgnore;
@ApiIgnore
@RestController
public class TestController {
	
	@Autowired
	private MyAccessTokenService myAccessTokenService;

	@Autowired
	private UserService UserService;
	
	@RequestMapping("/dologin")
	public Object dologin(@RequestBody User user) {
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
			System.out.println("username : " + user.getUsername());
			result.put("message", "登录成功，"+user.getPassword());
			result.put("data", "{ token: 'admin-token' }");
		} catch (Exception e) {
			result.put("status", 1);
			result.put("message", "请求失败，"+e.getMessage());
		}
//		{ code: 20000, data: { token: 'admin-token' } }
//		store ok, {"code":20000,"data":{"token":"admin-token"}}
		System.out.println("{ \"code\": 20000, \"data\": { \"token\": \"admin-token\" } }");
		return "{ \"code\": 20000, \"data\": { \"token\": \"admin-token\" } }";
	}
	
	@RequestMapping("/dologin2")
	public Object dologin2(@RequestBody User user) {
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
			System.out.println("用户正在请求dologin，username : " + user.getUsername());
			if(StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
				throw new Exception("账号密码不能为空");
			}
			//验证登录账号和密码的准确性
			String userAccount = user.getUsername();//登录账号
			User queryUser = new User();
			queryUser.setUserAccount(userAccount);
			User userCurrent = UserService.selectOne(queryUser);
			if(userCurrent == null) {
				throw new Exception("账号不存在");
			}else if(!user.getPassword().equals(userCurrent.getPassword())){
				throw new Exception("密码错误");
			}else {
				//...
			}
			Map<String,Object> tokenMap = new HashMap<String,Object>();
			String randomUUID = UUID.randomUUID().toString();//产生随机数作为token
			
			MyAccessToken myAccessToken = new MyAccessToken();
			myAccessToken.setAccess_token(randomUUID);
			myAccessToken.setUserAccount(userAccount);
			myAccessToken.setScope("all");
			result.put("message", myAccessToken);
			//将对应的username的AccessToken保存至服务器，将myAccessToken入库
			myAccessTokenService.create(myAccessToken);
			//重新申请accessTolen之后需要将旧的数据清除掉
			//...
			//生成返回值
			tokenMap.put("token", randomUUID);
			result.put("data", tokenMap);
		} catch (Exception e) {
			result.put("code", 1);
			result.put("message", "请求失败，"+e.getMessage());
		}
//		String rlt = "{ \"code\": 20000, \"data\": { \"token\": \"admin-token\" } }";
//		System.out.println(rlt);
//		return rlt;
		System.out.println(result);
		return result;
	}
	
}
